package com.koron.operation.classes.form.service.impl;


import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.BaseBean;
import com.koron.bean.query.OrderItem;
import com.koron.bean.query.PageQuery;
import com.koron.operation.classes.form.bean.convertor.ClassesFormRankConvertor;
import com.koron.operation.classes.form.bean.dto.ClassesFormRankDTO;
import com.koron.operation.classes.form.bean.po.ClassesFormRank;
import com.koron.operation.classes.form.bean.po.ClassesFormUser;
import com.koron.operation.classes.form.bean.query.ClassesFormUserQuery;
import com.koron.operation.classes.form.mapper.ClassesFormRankMapper;
import com.koron.operation.classes.form.service.ClassesFormRankService;
import com.koron.operation.classes.form.service.ClassesFormUserService;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 月度排班表岗位配置(ClassesFormRank)表服务实现类
 *
 * @author jzp
 * @since 2023-07-18 17:50:57
 */
@Service
public class ClassesFormRankServiceImpl implements ClassesFormRankService {

    @Autowired
    private ClassesFormRankConvertor classesFormRankConvertor;

    @Autowired
    private ClassesFormUserService classesFormUserService;

    /**
     * 通过ID查询单条数据
     */
    @Override
    @TaskAnnotation("getById")
    public ClassesFormRank getById(SessionFactory factory, String id) {
        ClassesFormRankMapper mapper = factory.getMapper(ClassesFormRankMapper.class);
        return mapper.selectById(id);
    }

    /**
     * 查询所有数据
     */
    @Override
    @TaskAnnotation("list")
    public List<ClassesFormRank> list(SessionFactory factory, ClassesFormRank classesFormRank) {
        ClassesFormRankMapper mapper = factory.getMapper(ClassesFormRankMapper.class);
        return mapper.selectList(classesFormRank);
    }

    @Override
    @TaskAnnotation("listDto")
    public List<ClassesFormRankDTO> listDto(SessionFactory factory, ClassesFormRank classesFormRankQuery) {
        PageQuery pageQuery = PageQuery.newListQuery();
        classesFormRankQuery.setStatus("0"); // 只查启用的
        List<ClassesFormRank> list = this.page(factory, pageQuery, classesFormRankQuery).getList();
        List<ClassesFormRankDTO> classesFormRankDTOS = classesFormRankConvertor.entityListToDtoList(list);
        if (CollUtil.isEmpty(classesFormRankDTOS)) {
            return classesFormRankDTOS;
        }
        ClassesFormUserQuery userQuery = new ClassesFormUserQuery();
        userQuery.setRefRankIds(classesFormRankDTOS.stream().map(BaseBean::getId).collect(Collectors.toList()));
        userQuery.setStatus("0"); // 只查启用的
        List<ClassesFormUser> classesFormUserList = classesFormUserService.page(factory, pageQuery, userQuery).getList();
        if (classesFormUserList == null) {
            return classesFormRankDTOS;
        }
        Map<String, List<ClassesFormUser>> classesFormUserListMap = classesFormUserList.stream()
                .filter(e -> e.getRefRankId() != null)
                .collect(Collectors.groupingBy(ClassesFormUser::getRefRankId));
        for (ClassesFormRankDTO classesFormRankDTO : classesFormRankDTOS) {
            List<ClassesFormUser> users = classesFormUserListMap.get(classesFormRankDTO.getId());
            classesFormRankDTO.setUsers(users);
        }
        // 只需要人员不为空的岗位数据
        return classesFormRankDTOS.stream().filter(e -> CollUtil.isNotEmpty(e.getUsers())).collect(Collectors.toList());
    }

    @Override
    @TaskAnnotation("page")
    public PageInfo<ClassesFormRank> page(SessionFactory factory, PageQuery pageQuery, ClassesFormRank classesFormRank) {
        pageQuery.setOrders(Collections.singletonList(new OrderItem("sort", true)));
        if (pageQuery.getPageNum() == 0) {
            PageHelper.orderBy(pageQuery.getOrderBy());
        } else {
            PageHelper.startPage(pageQuery);
        }
        List<ClassesFormRank> classesFormRankList = list(factory, classesFormRank);
        return new PageInfo<>(classesFormRankList);
    }

    /**
     * 新增数据
     */
    @Override
    @TaskAnnotation("save")
    public boolean save(SessionFactory factory, ClassesFormRank classesFormRank) {
        ClassesFormRankMapper mapper = factory.getMapper(ClassesFormRankMapper.class);
        return mapper.insertOrUpdate(classesFormRank) > 0;
    }

    /**
     * 修改数据
     */
    @Override
    @TaskAnnotation("update")
    public boolean update(SessionFactory factory, ClassesFormRank classesFormRank) {
        ClassesFormRankMapper mapper = factory.getMapper(ClassesFormRankMapper.class);
        return mapper.update(classesFormRank) > 0;
    }

    /**
     * 通过主键删除数据
     */
    @Override
    @TaskAnnotation("deleteById")
    public boolean deleteById(SessionFactory factory, String id) {
        ClassesFormRankMapper mapper = factory.getMapper(ClassesFormRankMapper.class);
        return mapper.hidden(id) > 0;
    }
}
